// 主题配置
export interface ThemeConfig {
  name: string
  label: string
  colors: {
    primary: string
    secondary: string
    success: string
    warning: string
    error: string
    info: string
    background: string
    surface: string
    text: string
    textSecondary: string
    border: string
    shadow: string
  }
}

// 预定义主题
export const themes: Record<string, ThemeConfig> = {
  light: {
    name: 'light',
    label: '浅色主题',
    colors: {
      primary: '#1890ff',
      secondary: '#722ed1',
      success: '#52c41a',
      warning: '#faad14',
      error: '#f5222d',
      info: '#13c2c2',
      background: '#f5f6fa',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#e0e0e0',
      shadow: 'rgba(0, 0, 0, 0.08)',
    },
  },
  dark: {
    name: 'dark',
    label: '深色主题',
    colors: {
      primary: '#177ddc',
      secondary: '#642ab5',
      success: '#49aa19',
      warning: '#d89614',
      error: '#d32029',
      info: '#08979c',
      background: '#1a1a1a',
      surface: '#2a2a2a',
      text: '#ffffff',
      textSecondary: '#b0b0b0',
      border: '#404040',
      shadow: 'rgba(0, 0, 0, 0.4)',
    },
  },
  blue: {
    name: 'blue',
    label: '蓝色主题',
    colors: {
      primary: '#1677ff',
      secondary: '#4096ff',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#f0f8ff',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#d6e4ff',
      shadow: 'rgba(22, 119, 255, 0.08)',
    },
  },
  green: {
    name: 'green',
    label: '绿色主题',
    colors: {
      primary: '#52c41a',
      secondary: '#73d13d',
      success: '#389e0d',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#f6ffed',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#b7eb8f',
      shadow: 'rgba(82, 196, 26, 0.08)',
    },
  },
  purple: {
    name: 'purple',
    label: '紫色主题',
    colors: {
      primary: '#722ed1',
      secondary: '#9254de',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#f9f0ff',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#d3adf7',
      shadow: 'rgba(114, 46, 209, 0.08)',
    },
  },
  orange: {
    name: 'orange',
    label: '橙色主题',
    colors: {
      primary: '#fa8c16',
      secondary: '#ffa940',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#fff7e6',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#ffd591',
      shadow: 'rgba(250, 140, 22, 0.08)',
    },
  },
  pink: {
    name: 'pink',
    label: '粉色主题',
    colors: {
      primary: '#eb2f96',
      secondary: '#f759ab',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#fff0f6',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#ffadd2',
      shadow: 'rgba(235, 47, 150, 0.08)',
    },
  },
  cyan: {
    name: 'cyan',
    label: '青色主题',
    colors: {
      primary: '#13c2c2',
      secondary: '#36cfc9',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#1890ff',
      background: '#e6fffb',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#87e8de',
      shadow: 'rgba(19, 194, 194, 0.08)',
    },
  },
  red: {
    name: 'red',
    label: '红色主题',
    colors: {
      primary: '#f5222d',
      secondary: '#ff4d4f',
      success: '#52c41a',
      warning: '#faad14',
      error: '#cf1322',
      info: '#13c2c2',
      background: '#fff1f0',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#ffa39e',
      shadow: 'rgba(245, 34, 45, 0.08)',
    },
  },
  gold: {
    name: 'gold',
    label: '金色主题',
    colors: {
      primary: '#faad14',
      secondary: '#ffc53d',
      success: '#52c41a',
      warning: '#d48806',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#fffbe6',
      surface: '#ffffff',
      text: '#1a1a1a',
      textSecondary: '#666666',
      border: '#ffe58f',
      shadow: 'rgba(250, 173, 20, 0.08)',
    },
  },
}

// 默认主题
export const defaultTheme = 'light'

// 获取主题配置
export const getThemeConfig = (themeName: string): ThemeConfig => {
  return themes[themeName] || themes[defaultTheme]
}

// 应用主题到 CSS 变量
export const applyTheme = (themeName: string) => {
  const theme = getThemeConfig(themeName)
  const root = document.documentElement

  // 设置 CSS 变量
  Object.entries(theme.colors).forEach(([key, value]) => {
    root.style.setProperty(`--theme-${key}`, value)
  })

  // 设置主题类名
  root.setAttribute('data-theme', themeName)
  
  // 保存到本地存储
  localStorage.setItem('theme', themeName)
}

// 初始化主题
export const initTheme = () => {
  const savedTheme = localStorage.getItem('theme') || defaultTheme
  applyTheme(savedTheme)
  return savedTheme
} 